package yl.hs.bmipfull.controllers.material;


import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import yl.hs.bmipfull.controllers.ControllerBase;
import yl.hs.bmipfull.pojo.material.PurchaseContractProductEntity;
import yl.hs.bmipfull.service.material.IPurchaseContractProductService;
import yl.hs.bmipfull.viewmodels.PageQuery;
import yl.hs.bmipfull.viewmodels.WebResult;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 作用:  采购合同产品明细信息控制层<br/>
 * 创建时间: 2021年06月03日 15时38分01秒 <br/>
 *
 * @author 张凯
 */
@Api(value = "采购合同产品明细信息Web接口", tags = "采购管理——采购合同产品明细信息操作")
@RequestMapping(value = "/api/purchaseContractProduct")
@RestController
public class PurchaseContractProductController extends ControllerBase {

    @Autowired
    IPurchaseContractProductService service;

    @ApiOperation(value = "查询采购合同产品明细信息", notes = "查询采购合同产品明细信息")
    @RequestMapping(value = "", method = RequestMethod.POST)
    public WebResult queryList(@ApiParam(value = "查询条件") @RequestBody PageQuery pageQuery) {
        try {
            return success("查询成功", service.queryList(pageQuery));
        } catch (Exception e) {
            return error(e);
        }
    }
    @ApiOperation(value = "查询采购合同产品明细信息", notes = "查询采购合同产品明细信息")
    @RequestMapping(value = "/query", method = RequestMethod.POST)
    public WebResult query(@ApiParam(value = "查询条件") @RequestBody PageQuery pageQuery) {
        try {
            var result =  service.queryList(pageQuery);
            var rmGroup = result.stream().collect(Collectors.groupingBy(t->t.getRmSN()));
            List<PurchaseContractProductEntity> data = new ArrayList<>();
            for (var rmSN : rmGroup.keySet()) {
                 var rlist = rmGroup.get(rmSN);
                 if(rlist.size()>0){
                     data.add(rlist.get(0));
                 }
            }
            return success("查询成功", data);
        } catch (Exception e) {
            return error(e);
        }
    }
    @ApiOperation(value = "根据合同编号查询采购合同产品明细信息", notes = "根据合同编号查询采购合同产品明细信息")
    @RequestMapping(value = "/{contractSn}", method = RequestMethod.GET)
    public WebResult queryListByContractSn(@ApiParam(value = "合同编号") @PathVariable String contractSn) {
        try {
            return success("查询成功", service.queryListByContractSn(contractSn));
        } catch (Exception e) {
            return error(e);
        }
    }

    @ApiOperation(value = "删除采购合同产品明细信息信息", notes = "根据采购合同产品明细信息编号删除系采购合同产品明细信息信息")
    @RequestMapping(path = "", method = RequestMethod.DELETE)
    public WebResult delete(@ApiParam(value = "采购合同产品明细信息编号", name = "sn", allowMultiple = true, required = true) @RequestParam("sn") String[] sn) {
        if (service.delete(sn) > 0) {
            return success("删除成功");
        } else {
            return fail("删除失败");
        }
    }
    @ApiOperation(value = "禁用该合同产品", notes = "根据采购合同产品明细信息编号禁用该合同产品")
    @RequestMapping(path = "/updatestate/{sn}/{state}", method = RequestMethod.GET)
    public WebResult updateStateBySn(@ApiParam(value = "采购合同产品明细信息编号", name = "sn", allowMultiple = true, required = true) @PathVariable("sn") String sn,@ApiParam(value = "采购合同产品明细信息状态", name = "state", allowMultiple = true, required = true) @PathVariable("state") int state) {
        if (service.updateStateBySn(sn,state) > 0) {
            return success("修改成功");
        } else {
            return fail("修改失败");
        }
    }
}